const Token = require("../utils/Token");
const { poolQuery, randomString, toTime } = require("../utils/utils");
const TOKEN_EXPRESS_SENCOND = 360000000; // token有效期
/** 登录*/
const login = async (req, res) => {
  const sql = `select * from tyz_user where phone='${req.body.phone}' and password='${req.body.password}'`;
  const result = await poolQuery(sql, res);
  // console.log(req);
  if (result && result.length > 0) {
    result[0].token = Token.encrypt({
      id: result[0].id,
      TOKEN_EXPRESS_SENCOND,
    });
    res.send({
      code: 200,
      starts: "success",
      message: "登录成功",
      data: result,
    });
    console.log(result);
  } else {
    res.send({
      code: 500,
      starts: "error",
      message: "登录失败",
    });
  }
};
/** 查询用户信息*/
const getUsers = async (req, res) => {
  const sql = `SELECT * FROM tyz_user where phone = '${req.query.phone}'`;
  const result = await poolQuery(sql, res);
  if (result && result.length > 0) {
    res.send({
      code: 200,
      starts: "success",
      message: "查询成功",
      data: result,
    });
  } else {
    res.send({
      code: 500,
      starts: "error",
      message: "查询失败",
    });
  }
};
/** 保存图片*/
const addImage = async (req, res) => {
  // http://47.104.71.122:8088${req.body.url}
  const sql = `INSERT INTO tyz_photo (photo,photo_name, phone, create_time, is_shoucang )
VALUES
	('http://127.0.0.1:8088${req.body.url}' , '${req.body.photo_name}',
	'${req.body.phone}','${toTime()}', '0' )`;
  await poolQuery(sql, res);
  res.send({
    code: 200,
    starts: "success",
    message: "上传成功",
  });
};
/** 查询出用户上传的全部图片*/
const getAllPhoto = async (req, res) => {
  const avatar = `,(SELECT photo from tyz_user WHERE phone = tyz_photo.phone) as avatar`;
  const nick_name = `,(SELECT nick_name from tyz_user WHERE phone = tyz_photo.phone) as nick_name`;
  const like_count = `, (SELECT COUNT(*) FROM tyz_like_photo where phone = tyz_photo.phone and photo_id = tyz_photo.id) as like_count`;
  const sql = `SELECT * ${avatar} ${nick_name} ${like_count} FROM tyz_photo where delete_time is NULL ORDER BY create_time DESC`;
  console.log(sql);
  const result = await poolQuery(sql, res);
  if (result && result.length > 0) {
    res.send({
      code: 200,
      starts: "success",
      message: "查询成功",
      data: result,
    });
  } else {
    res.send({
      code: 500,
      starts: "error",
      message: "查询失败",
    });
  }
};
/** 查询个人中心 自己的作品和收藏 */
const getUserPhotoFollow = async (req, res) => {
  const sql = `SELECT * FROM tyz_photo where phone = '${req.query.phone}' and is_shoucang = ${req.query.type} and delete_time is NULL ORDER BY create_time desc`;
  console.log(sql);
  const result = await poolQuery(sql, res);
  if (result && result.length >= 0) {
    res.send({
      code: 200,
      starts: "success",
      message: "查询成功",
      data: result,
    });
  } else {
    res.send({
      code: 500,
      starts: "error",
      message: "查询失败",
      error: sql,
    });
  }
};
/** 删除个人中心的个人作品 */
const delUserContent = async (req, res) => {
  const sql = `UPDATE tyz_photo SET delete_time = '${toTime()}' WHERE phone = '${
    req.query.phone
  }' AND id = '${req.query.id}'`;
  console.log(sql);
  const result = await poolQuery(sql, res);
  if (result) {
    res.send({
      code: 200,
      starts: "success",
      message: "删除成功",
    });
  } else {
    res.send({
      code: 500,
      starts: "error",
      message: "删除失败",
    });
  }
};
/** 喜欢图片设置 */
const setPhotoLike = async (req, res) => {
  const { phone, photo_id, photo_url, who_phone } = req.body;
  const sql = `INSERT INTO tyz_like_photo(phone,photo_id,is_like,photo_url,who_like_phone) VALUES('${phone}','${photo_id}','1','${photo_url}','${who_phone}')`;
  const result = await poolQuery(sql, res);
  if (result) {
    res.send({
      code: 200,
      starts: "success",
      message: "收藏成功",
    });
  } else {
    res.send({
      code: 500,
      starts: "error",
      message: "收藏失败",
    });
  }
};
module.exports = {
  login,
  getUsers,
  addImage,
  getAllPhoto,
  getUserPhotoFollow,
  delUserContent,
  setPhotoLike,
};

/** 引入文件内方法：需要去   /routes/users.js  中引入*/
/** 引入文件内方法：需要去   /routes/users.js  中引入*/
/** 引入文件内方法：需要去   /routes/users.js  中引入*/
